<?php
//权限管理
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Controller\AdminController;

class MemberController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('action', 'member');
        $this->assign('sys', 'dashboard'); // 数据集
    }
//管理员列表
    public function index() {
        $list = M('member')->select();
        $this->assign('list', $list);
        $this->display();
    }

    //添加管理员
    public function add() {

        if (IS_POST) {
            list($password,$salt) = password(I('password'));
            $dates['password']=$password;
            $dates['salt']=$salt;
            $dates['group_id']=I('group_id');
            $dates['truename']=I('truename');
            $dates['mobile']=I('mobile');
            $dates['email']=I('email');
            $dates['created']=  time();
            $dates['status']=I('status');
            $re = M('member')->add($dates);
            if ($re) {
                $role[] = array(
                    'role_id' => $dates['group_id'],
                    'user_id' => $re,
                );
                //mysql,支持addAll
                if (in_array(strtolower(C('DB_TYPE')), array('mysql', 'mysqli', 'mongo'))) {
                    M('role_user')->addAll($role);
                } else {
                    foreach ($role as $v) {
                        M('role_user')->add($v);
                    }
                }
                
                //日志记录
                $logobj = new \Libs\Logs();
                $html = "添加：" . I('truename') . "--管理员";
                $logobj->write(array("管理员:".session('manager_auth.username'),"后台管理员",$html));
                $this->success("用户添加成功！", U('index'));
            } else {
                $this->error("添加用户出错");
            }
        }

        $group = M('role')->select();
        $this->assign('group', $group);
        $this->display();
    }

    //修改管理员
    function edit() {
        $id = I('id');
        $uModel = M('member');
        if (empty($id) | $id <= 0) {
            $this->error('非法的操作', U('index'));
        }
        $date = M('member')->where('id=%d',$id)->find();
        if(IS_POST){
            if(I('password') != $date['password']){
                list($password,$salt) = password(I('password'));
                $dates['password']=$password;
                $dates['salt']=$salt;
            }
            
            $dates['truename']=I('truename');
            $dates['group_id']=I('group_id');
            $dates['mobile']=I('mobile');
            $dates['email']=I('email');
            $dates['status']=I('status');
            $resule = $uModel->where('id=%d', $id)->save($dates);
            if (false !== $resule) {
                //修改RBAC模式
                $role[] = array(
                    'role_id' => $dates['group_id'],
                    'user_id' => $id,
                );
                M('role_user')->where(array('user_id' => $id))->delete();
                //mysql,支持addAll
                if (in_array(strtolower(C('DB_TYPE')), array('mysql', 'mysqli', 'mongo'))) {
                    M('role_user')->addAll($role);
                }else{
                    foreach ($role as $v) {
                        M('role_user')->add($v);
                    }
                }
                //记录操作日志
                //日志记录
                $logobj = new \Libs\Logs();
                $html = "修改：" . I('truename') . "--管理员";
                $logobj->write(array("管理员:".session('manager_auth.username'),"添加后台管",$html));
                $this->success('修改成功！正在跳转……', U('index'));
            } else {
                $this->error('修改失败~~~ 正在返回……');
            }
        }
        $group = M('role')->select();
        $this->assign('group', $group);
        $this->assign('date', $date);
        $this->display();
    }

    function del() {
        $u_id = I('post.data', 0, 'intval');
        $uModel = M('member');
        $name = $uModel->where('id=%d', $u_id)->getField('truename');
        if ($uModel->where('id=%d', $u_id)->delete()) {
            //日志记录
            $logobj = new \Libs\Logs();
            $html = "删除：" . $name . "--管理员";
            $logobj->write(array("管理员:".session('manager_auth.username'),"后台管理员",$html));

            $this->success('删除成功！正在跳转……');
        } else {
            $this->error('删除失败！正在返回……');
        }
    }
    
    //修改密码
    public function editpwd(){
        if(IS_POST){
            $pwd = I('password');
            $pwd1 = I('password1');
            if($pwd == $pwd1){
                list($password,$salt) = password($pwd);
                $dates['password']=$password;
                $dates['salt']=$salt;
                $where['id'] = session('manager_id');
                $re = M('member')->where($where)->save($dates);
                if($re !== FALSE){
                    session('[destroy]');
                    $this->success('修改成功！');
                }else{
                    $this->error('修改失败！');
                }
            }else{
                $this->error('两次输入的密码不一致！');
            }
        }
        
        $id = session('manager_id');
        $uModel = M('member');
        if (empty($id) | $id <= 0) {
            $this->error('非法的操作', U('index'));
        }
        $date = M('member')->where('id=%d',$id)->find();
        $this->assign('date', $date);
        $this->display();
    }
    
}
